﻿using System;
using System.Data;
using System.Text;
using Enterprise.DataFactory;
using Enterprise.Model.Jihuabu;
namespace Enterprise.Data.Jihuabu
{
    /// <summary>
    /// 数据访问类:M01_JDBData
    /// </summary>
    public partial class M01_JDBData
    {
        public M01_JDBData()
        { }
        #region  Method


        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string AFEBH, string WCSJ)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from JHB_M01_JDB");
            strSql.Append(" where AFEBH='" + AFEBH + "' and WCSJ='" + WCSJ + "' ");
            return DbExecute.Exist(strSql.ToString());
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(Model.Jihuabu.M01_JDBEntity model)
        {
            StringBuilder strSql = new StringBuilder();
            StringBuilder strSql1 = new StringBuilder();
            StringBuilder strSql2 = new StringBuilder();
            if (model.AFEBH != null)
            {
                strSql1.Append("AFEBH,");
                strSql2.Append("'" + model.AFEBH + "',");
            }
            if (model.WCSJ != null)
            {
                strSql1.Append("WCSJ,");
                strSql2.Append("'" + model.WCSJ + "',");
            }
            if (model.WC_JG != null)
            {
                strSql1.Append("WC_JG,");
                strSql2.Append("'" + model.WC_JG + "',");
            }
            if (model.LRR != null)
            {
                strSql1.Append("LRR,");
                strSql2.Append("'" + model.LRR + "',");
            }
            if (model.LRSJ != null)
            {
                strSql1.Append("LRSJ,");
                strSql2.Append("to_date('" + model.LRSJ.ToString() + "','YYYY-MM-DD HH24:MI:SS'),");
            }
            strSql.Append("insert into JHB_M01_JDB(");
            strSql.Append(strSql1.ToString().Remove(strSql1.Length - 1));
            strSql.Append(")");
            strSql.Append(" values (");
            strSql.Append(strSql2.ToString().Remove(strSql2.Length - 1));
            strSql.Append(")");
            int rows = DbExecute.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Model.Jihuabu.M01_JDBEntity model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update JHB_M01_JDB set ");
            if (model.WC_JG != null)
            {
                strSql.Append("WC_JG='" + model.WC_JG + "',");
            }
            else
            {
                strSql.Append("WC_JG= null ,");
            }
            if (model.LRR != null)
            {
                strSql.Append("LRR='" + model.LRR + "',");
            }
            else
            {
                strSql.Append("LRR= null ,");
            }
            if (model.LRSJ != null)
            {
                strSql.Append("LRSJ=to_date('" + model.LRSJ.ToString() + "','YYYY-MM-DD HH24:MI:SS'),");
            }
            else
            {
                strSql.Append("LRSJ= null ,");
            }
            int n = strSql.ToString().LastIndexOf(",");
            strSql.Remove(n, 1);
            strSql.Append(" where AFEBH='" + model.AFEBH + "' and WCSJ='" + model.WCSJ + "' ");
            int rowsAffected = DbExecute.ExecuteSql(strSql.ToString());
            if (rowsAffected > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(string AFEBH, string WCSJ)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from JHB_M01_JDB ");
            strSql.Append(" where AFEBH='" + AFEBH + "' and WCSJ='" + WCSJ + "' ");
            int rowsAffected = DbExecute.ExecuteSql(strSql.ToString());
            if (rowsAffected > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.Jihuabu.M01_JDBEntity GetModel(string AFEBH, string WCSJ)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  ");
            strSql.Append(" AFEBH,WCSJ,WC_JG,LRR,LRSJ ");
            strSql.Append(" from JHB_M01_JDB ");
            strSql.Append(" where AFEBH='" + AFEBH + "' and WCSJ='" + WCSJ + "' ");
            Model.Jihuabu.M01_JDBEntity model = new M01_JDBEntity();
            DataSet ds = DbExecute.GetDataSet(strSql.ToString());
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.Jihuabu.M01_JDBEntity DataRowToModel(DataRow row)
        {
            Model.Jihuabu.M01_JDBEntity model = new Model.Jihuabu.M01_JDBEntity();
            if (row != null)
            {
                if (row["AFEBH"] != null)
                {
                    model.AFEBH = row["AFEBH"].ToString();
                }
                if (row["WCSJ"] != null)
                {
                    model.WCSJ = row["WCSJ"].ToString();
                }
                if (row["WC_JG"] != null)
                {
                    model.WC_JG = row["WC_JG"].ToString();
                }
                if (row["LRR"] != null)
                {
                    model.LRR = row["LRR"].ToString();
                }
                if (row["LRSJ"] != null && row["LRSJ"].ToString() != "")
                {
                    model.LRSJ = DateTime.Parse(row["LRSJ"].ToString());
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select AFEBH,WCSJ,WC_JG,LRR,LRSJ ");
            strSql.Append(" FROM JHB_M01_JDB ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbExecute.GetDataSet(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM JHB_M01_JDB ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbExecute.ExecuteScalar(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.WCSJ desc");
            }
            strSql.Append(")AS R, T.*  from JHB_M01_JDB T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.R between {0} and {1}", startIndex, endIndex);
            return DbExecute.GetDataSet(strSql.ToString());
        }

        /*
        */

        #endregion  Method
        #region  MethodEx

       /// <summary>
       /// 获取已录入周期
       /// </summary>
       /// <returns></returns>
        public DataSet getRiqiList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select distinct(WCSJ) as WCSJ ");
            strSql.Append(" FROM JHB_M01_JDB ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by WCSJ DESC");
            return DbExecute.GetDataSet(strSql.ToString());
        }

        /// <summary>
        /// 执行SQL返回DataSet
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public DataSet GetListbySQL(string sql)
        {
            return DbExecute.GetDataSet(sql);
        }
        
        /// <summary>
        /// 更新sql语句
        /// </summary>
        /// <param name="sql"></param>
        public void ExcuteUpdateSql(string sql)
        {
            DbExecute.Execute(sql);
        }

        
        /// <summary>
        /// 从项目表创建数据
        /// </summary>
        public void CreateDataFromXmb(string rq)
        {
            string sql = "select AFEBH from jhb_m01_xmb where zt=0";
            DataSet ds = DbExecute.GetDataSet(sql);
            string updatesql = "begin ";
            updatesql += "delete from jhb_m01_jdb where wcsj='" + rq + "';";
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                StringBuilder strSql = new StringBuilder();
                StringBuilder strSql1 = new StringBuilder();
                StringBuilder strSql2 = new StringBuilder();
                if (dr["AFEBH"] != null)
                {
                    strSql1.Append("AFEBH,");
                    strSql2.Append("'" + dr["AFEBH"] + "',");
                }
                if (rq != null)
                {
                    strSql1.Append("WCSJ,");
                    strSql2.Append("'" + rq + "',");
                }                
                strSql.Append("insert into JHB_M01_JDB(");
                strSql.Append(strSql1.ToString().Remove(strSql1.Length - 1));
                strSql.Append(")");
                strSql.Append(" values (");
                strSql.Append(strSql2.ToString().Remove(strSql2.Length - 1));
                strSql.Append(");");
                updatesql += strSql.ToString();                
            }
            updatesql += "null;end;";
            DbExecute.Execute(updatesql);
        }

        #endregion  MethodEx

        /// <summary>
        /// 是否是新的
        /// </summary>
        /// <param name="rq"></param>
        /// <returns></returns>
        public bool isNew(string rq)
        {
            string sql = "select max(wcsj) from jhb_m01_jdb ";
            object o = DbExecute.ExecuteScalar(sql);
            if (o != null && !string.IsNullOrEmpty(o.ToString()))
            {
                if (o.ToString() == rq)
                    return true;
                else
                    return false;
            }
            else
                return true;
        }
    }
}

